package user

import (
	"github.com/gin-gonic/gin"
	log "github.com/star-find-cloud/star-chat/pkg/logger"
	"github.com/star-find-cloud/star-chat/user/domain"
	"github.com/star-find-cloud/star-chat/user/dto"
	"github.com/star-find-cloud/star-chat/utils"
	"net/http"
)

type UserHandler struct {
	service domain.UserService
}

func NewUserHandler(service domain.UserService) *UserHandler {
	return &UserHandler{
		service: service,
	}
}

// TODO: 接口文档
func (h *UserHandler) Create(c *gin.Context) {
	var req = &dto.RegisterUserReq{}
	if err := c.ShouldBind(req); err != nil {
		log.HttpLogger.Errorf("request error: %v", err)
		utils.RespondError(c, http.StatusBadRequest, "request parameter error")
		return
	}

	user := &domain.User{}
	if err := utils.ConversionStructForJson(req, user); err != nil {
		log.AppLogger.Errorf("conversion json error: %v", err)
	}

	id, err := h.service.Create(c, user)
	if err != nil {
		utils.RespondError(c, http.StatusInternalServerError, "internal server error")
		return
	}

	utils.RespondJSON(c, http.StatusOK, id)
	return
}

func (h *UserHandler) Register(c *gin.Context) {
	var req = &dto.RegisterUserReq{}
	if err := c.ShouldBind(req); err != nil {
		log.HttpLogger.Errorf("request error: %v", err)
		utils.RespondError(c, http.StatusBadRequest, "请求参数错误")
		return
	}

	exist, err := h.service.CheckEmailVerificationCode(c, req.Email, req.VerificationCode)
	if err != nil || !exist {
		utils.RespondError(c, http.StatusBadRequest, "verification code err")
		return
	}

	user := &domain.User{}
	if err := utils.ConversionStructForJson(req, user); err != nil {
		log.AppLogger.Errorf("conversion json error: %v", err)
	}

	token, id, err := h.service.Register(c, user)
	if err != nil {
		utils.RespondError(c, http.StatusInternalServerError, "服务器内部错误")
		return
	}

	utils.RespondJSON(c, http.StatusOK, &dto.RegisterUserRespon{
		ID:    id,
		Token: token,
	})
	return
}

// Get 获取用户信息
func (h *UserHandler) Get(c *gin.Context) {

}

func (h *UserHandler) Login(c *gin.Context) {

}

func (h *UserHandler) GetByID(c *gin.Context) {

}

func (h *UserHandler) GetByEmail(c *gin.Context) {}

func (h *UserHandler) GetByPhone(c *gin.Context) {

}

func (h *UserHandler) Update(c *gin.Context) {

}

func (h *UserHandler) UpdatePassword(c *gin.Context) {

}

func (h *UserHandler) UpdatePhone(c *gin.Context) {

}

func (h *UserHandler) ForgetPassword(c *gin.Context) {

}

// BannedUser 封禁用户
func (h *UserHandler) BannedUser(c *gin.Context) {

}

func (h *UserHandler) Delete(c *gin.Context) {

}
